package com.hq.door.controller;

import java.beans.PropertyEditorSupport;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hq.door.dto.OutrequestInfo;
import com.hq.door.dto.TRecordInfo;
import com.hq.door.pojo.TStudent;
import com.hq.door.pojo.TTeacher;
import com.hq.door.service.IStudentFrontService;
import com.hq.door.util.imageCodeUntil;
import com.hq.door.util.staticErrorCode;

/**
 * 学生端控制器
 * 
 * @author Administrator
 * 
 */
@Controller
@RequestMapping("/StudentFront")
public class StudentFrontController {
	@Autowired
	IStudentFrontService studentFrontService;

	/**
	 * 自动转化日期类型参数
	 * 
	 * @param binder
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		// 日期处理
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		df.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
		binder.registerCustomEditor(Timestamp.class,
				new PropertyEditorSupport() {
					@Override
					public void setAsText(String value) {
						setValue(new Timestamp(Long.valueOf(value)));
					}
				});
	}

	/**
	 * 微信ID验证，实现自动登录
	 * 
	 * @param session
	 * @return
	 * 跳转页面
	 */
	@RequestMapping("/weChatCheck")
	public String weChatCheck(HttpSession session) {
		String wexinId = (String) session.getAttribute("wexinId");
		if (wexinId != null && !"".equals(wexinId)) {
			TStudent tStudent = studentFrontService.weChatLogin(wexinId);
			if (tStudent != null) {
				session.setAttribute("loginUser", tStudent);
				return "redirect:/front_end/weChat/student/card_weChat_student.html";
			} else {
				return "redirect:/front_end/weChat/student/login_weChat_student.html";
			}
		}
		return null;
	}

	/**
	 * 学生端微信登录
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("/weChatLogin")
	@ResponseBody
	public Object weChatLogin(@RequestParam String name, @RequestParam String password,HttpSession session){
			String weixinId = (String) session.getAttribute("wexinId");
			TStudent tStudent =studentFrontService.modifyStudent(name,password, weixinId);
			if (tStudent != null) {
				session.setAttribute("loginUser", tStudent);
				return staticErrorCode.error_code_success;
			} else {
				return staticErrorCode.error_code_mismatch_wxBind_fail;
			}
	}
	
	
	/**
	 * 验证码生成
	 * @param response
	 * @param request
	 */
	@RequestMapping("/imageCode")
	public void getImageCode(HttpServletResponse response,
			HttpServletRequest request) {
		response.setContentType("image/jpeg");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Set-Cookie", "name=value; HttpOnly");
		response.setDateHeader("Expire", 0);
		imageCodeUntil.getRandcode(request, response, "imagecode");
	}
	/**
	 * 登陆验证
	 * @param session
	 * @param code
	 * 学号
	 * @param password
	 * 密码
	 * @param verifyCode
	 * 验证码
	 * @return
	 * 返回成功或失败消息
	 */
	@RequestMapping("/check")
	@ResponseBody
	public Object check(HttpSession session, String code, String password,
			String verifyCode) {
		String sessionVerifyCode = (String) session.getAttribute(session
				.getId() + "imagecode");
		if (sessionVerifyCode != null
				&& sessionVerifyCode.equals(verifyCode.toUpperCase())) {
			TStudent tStudent = studentFrontService.login(code, password);
			if (tStudent != null) {
				session.setAttribute("loginUser", tStudent);
				return staticErrorCode.error_code_success;
			} else {
				return staticErrorCode.error_code_mismatch_user_or_password;
			}
		} else {
			return staticErrorCode.error_code_mismatch_verifyCode;
		}
	}
	/**
	 * 获取当前登录学生信息
	 * @param session
	 * @return
	 * 返回学生信息dto
	 */
	@RequestMapping("/getCurStu")
	@ResponseBody
	public Object getCurStu(HttpSession session) {
		TStudent tStudent = (TStudent) session.getAttribute("loginUser");
		return studentFrontService.getCurStu(tStudent);
	}
	/**
	 * 分页查询刷卡记录
	 * @param session
	 * @param sDate
	 * 开始时间
	 * @param eDate
	 * 结束时间
	 * @param pageSize
	 * 页面大小
	 * @param pageStart
	 * 页面开始位置
	 * @param type
	 * 查询类型
	 * @return
	 * 返回记录dto的list或者返回list大小
	 */
	@RequestMapping("/getCardRecords")
	@ResponseBody
	public Object getCardRecords(
			HttpSession session,
			@RequestParam(value = "startDate", required = true) Date sDate,
			@RequestParam(value = "endDate", required = true) Date eDate,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "pageStart", required = false) Integer pageStart,
			@RequestParam(value = "type", required = true) Integer type) {

		TStudent tStudent = (TStudent) session.getAttribute("loginUser");

		if (pageSize != null && pageStart != null) {
			List<TRecordInfo> tRecordInfos = studentFrontService
					.getCardRecords(tStudent, sDate, eDate, pageSize,
							pageStart, type);
			return tRecordInfos;
		} else {
			int res = studentFrontService
					.getCount(tStudent, sDate, eDate, type);
			return res;
		}
	}
	/**
	 * 获取外出申请记录
	 * @param session
	 * @param pageSize
	 * 页面大小
	 * @param pageStart
	 * 记录开始位置
	 * @return
	 * 返回申请记录或者返回申请记录条数
	 */
	@RequestMapping("/getOutRequest")
	@ResponseBody
	public Object getOutRequest(
			HttpSession session,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "pageStart", required = false) Integer pageStart) {

		TStudent tStudent = (TStudent) session.getAttribute("loginUser");

		if (pageSize != null && pageStart != null) {
			List<OutrequestInfo> outrequestInfos = studentFrontService
					.getOutRequest(tStudent, pageSize, pageStart);
			return outrequestInfos;
		} else {
			int res = studentFrontService.getRequestCount(tStudent);
			return res;
		}
	}
	/**
	 * 	添加外出申请记录
	 * @param session
	 * @param outrequestInfo
	 * 页面表单信息
	 * @return
	 * 返回成功或者失败
	 */
	@RequestMapping("/addOutRequest")
	@ResponseBody
	public String addOutRequest(HttpSession session,
			@RequestBody OutrequestInfo outrequestInfo) {

		TStudent tStudent = (TStudent) session.getAttribute("loginUser");

		int res = studentFrontService.addOutRequest(tStudent, outrequestInfo);
		if (res == 1) {
			return staticErrorCode.error_code_success;
		} else {
			return staticErrorCode.error_code_add_out_request_fail;
		}
	}
	/**
	 * 退出登录
	 * @param session
	 */
	@RequestMapping("/exit")
	@ResponseBody
	public void exit(HttpSession session) {
		session.removeAttribute("loginUser");
	}
}
